#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;


 // Definition for a binary tree node.
 struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
 //使用递归
// class Solution {
// public:
//     vector<int> inorderTraversal(TreeNode* root) {
//         vector<int> result;
//         if(root == nullptr){
//             return result;
//         }
//         func(root,result);
//         return result;

//     }
//     void func(TreeNode* root,vector<int>& result){
//         if(root == nullptr){
//             return;
//         }
//         func(root->left,result);
//         result.push_back(root->val);
//         func(root->right,result);
//     }
// };

//不使用递归
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> result;
        if(root == nullptr){
            return result;
        }
        stack<TreeNode*> S;
        S.push(root);
        TreeNode* curr = root->left;
        while(curr!=nullptr||!S.empty()){
            if(curr!=nullptr){
                S.push(curr);
                curr = curr->left;
            }
            else{
                TreeNode* temp = S.top();
                S.pop();
                result.push_back(temp->val);
                curr = temp->right;
            }
        }
        return result;
    }
};
